//
//  ReportViewController.m
//  UniversalApp
//
//  Created by 志刚杨 on 2018/3/17.
//  Copyright © 2018年 voidcat. All rights reserved.
//

#import "ReportViewController.h"
#import "HXPhotoViewController.h"
#import "HXPhotoView.h"
#import "ZHPickView.h"
#import "ReportModel.h"
#define _borderColor 0xE8E8E8
#define _backColor   0xF6F6F6
@interface ReportViewController ()<ZHPickViewDelegate,HXPhotoViewDelegate>
@property UIView *navView;
@property QMUIButton *saveBtn;
@property NSArray *arrlist;
@property NSInteger gindex;
@property (nonatomic,strong) ZHPickView *pickView;
@property (strong, nonatomic) HXPhotoManager *manager;
@property NSArray *uploadImage;
@property (nonatomic,strong) UIView *toolbar;
@property (nonatomic,strong) QMUITextView *textView;
@property (nonatomic,strong) NSString *type;
@property(nonatomic, strong) NSArray *reportList;

@end

@implementation ReportViewController
{
    UITextField *txtSchool;
    QMUILabel *plsSelect;
    YYTextKeyboardManager *YYmanager;
    
    
}

- (HXPhotoManager *)manager
{
    if (!_manager) {
        _manager = [[HXPhotoManager alloc] initWithType:HXPhotoManagerSelectedTypePhotoAndVideo];
        _manager.photoMaxNum = 1;//选择照片个数
        _manager.videoMaxNum = 0;
        _manager.selectTogether = NO;
        _manager.outerCamera = YES;
    }
    return _manager;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self getReportType];
    self.view.backgroundColor = [UIColor colorWithRGB:_backColor];
    //请选择
    UIView *selectView = [[UIView alloc] init];
    selectView.frame = CGRectMake(0, 10, SCREEN_WIDTH, 50);
    selectView.layer.borderColor = [UIColor colorWithRGB:_borderColor].CGColor;
    selectView.backgroundColor = [UIColor whiteColor];
    selectView.layer.borderWidth = 1;
    
    UITapGestureRecognizer *tapGesturRecognizer=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    [selectView addGestureRecognizer:tapGesturRecognizer];
    
    QMUILabel *lab = [[QMUILabel alloc] init];
    [selectView addSubview:lab];
    lab.text = ASLocalizedString(@"举报类型");
    [selectView addSubview:lab];
    [self.view addSubview:selectView];
    [lab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(selectView);
        make.left.equalTo(selectView).offset(10);
    }];
    
    //请选择
    plsSelect = [[QMUILabel alloc] init];
    [selectView addSubview:plsSelect];
    plsSelect.text = ASLocalizedString(@"请选择");
    [selectView addSubview:plsSelect];
    [self.view addSubview:selectView];
    plsSelect.textColor = RGB(22, 22, 22);
    plsSelect.font = DEFAULT_FONT(15);
    [plsSelect mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(selectView);
        make.right.equalTo(selectView).offset(-16);
    }];
    
    _textView = [[QMUITextView alloc] init];
    _textView.placeholder = ASLocalizedString(@"请填写举报内容");
    _textView.font = DEFAULT_FONT(13);
    [self.view addSubview:_textView];
    [_textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(selectView.mas_bottom).offset(10);
        make.left.equalTo(self.view).offset(10);
        make.right.equalTo(self.view).offset(-10);
        make.height.equalTo(@160);
        
    }];
    
    
    //示例
    QMUILabel *labeg = [[QMUILabel alloc] init];
    labeg.frame = CGRectMake(0, 120, 0, 0);
    labeg.text = @"";
    [self.view addSubview:labeg];
    labeg.textAlignment = NSTextAlignmentCenter;
    [labeg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(@170);
        make.width.equalTo(self.view);
    }];
    
    //选择照片视图
    HXPhotoView *photoView = [HXPhotoView photoManager:self.manager];
    CGFloat width = KScreenWidth;
    photoView.frame = CGRectMake(24, 300, width - 24, 0);
    photoView.delegate = self;
    photoView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:photoView];
    [self.view addSubview:_toolbar];
    
    [self addNavigationItemWithTitles:@[ASLocalizedString(@"提交   ")] isLeft:NO target:self action:@selector(save) tags:@[@1]];
}

- (void)photoViewChangeComplete:(NSArray<HXPhotoModel *> *)allList Photos:(NSArray<HXPhotoModel *> *)photos Videos:(NSArray<HXPhotoModel *> *)videos Original:(BOOL)isOriginal
{
    [HXPhotoTools fetchOriginalForSelectedPhoto:photos completion:^(NSArray<UIImage *> *images) {
        NSLog(@"%@",images);
        _uploadImage =  images;
    }];
}

- (void)photoViewUpdateFrame:(CGRect)frame WithView:(UIView *)view
{
    NSLog(@"%@",NSStringFromCGRect(frame));
}

-(void)tapAction:(id)tap
{
    [self showList];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden=YES;
    self.navigationController.navigationBar.hidden = NO;
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [_pickView remove];
}
-(void)showList
{
    //筛选出举报列表
    NSMutableArray *_arrlist = [NSMutableArray array];
    for (ReportModel *item in _reportList) {
        [_arrlist addObject:item.title];
    }
//    _arrlist  = [NSArray arrayWithArray:_reportList];
    _pickView=[[ZHPickView alloc] initPickviewWithArray:_arrlist isHaveNavControler:NO];
    _pickView.delegate = self;
    [_pickView show];
}
-(void)toobarDonBtnHaveClick:(ZHPickView *)pickView resultString:(NSString *)resultString{
    NSInteger index = [_arrlist indexOfObject:resultString];
//    index++;
    _gindex = index;
    [plsSelect setTextColor:[UIColor colorWithRGB:0x000000]];
    plsSelect.text = resultString;
    _type = resultString;
}
-(void)setNav
{
    
}

//获取举报类型
- (void)getReportType {
    NSString *url = [[CYURLUtils sharedCYURLUtils] makeURLWithC:@"page_data_api" A:@"get_report_type"];
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    //[param setObject:@"" forKey:@""];
    
    [CYNET POSTv2:url parameters:param responseCache:^(id responseObject) {
        
    } success:^(id responseObject) {
        _reportList = [NSArray modelArrayWithClass:[ReportModel class] json:[responseObject valueForKey:@"list"]];
        
    } failure:^(NSString *error) {
        [MBProgressHUD showTopTipMessage:error];
    } hasCache:NO];

}


-(void)save
{

//    [HUDHelper alert:ASLocalizedString(@"您的举报我们已经收到")];
    NSString *body = _textView.text;
    if(body.length <1)
    {
        body = @"";
    }

    if(_type.length <1 || _type == nil)
    {
        [MBProgressHUD showErrorMessage:ASLocalizedString(@"请选择举报类型")];
        return;
    }
    
    [[HUDHelper sharedInstance] syncLoading];
    NSString *url = [[CYURLUtils sharedCYURLUtils] makeURLWithC:@"user_api" A:@"do_report_user"];
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    
    [param setObject:curUser.id forKey:@"uid"];
    [param setObject:curUser.token forKey:@"token"];
    ReportModel *model =  _reportList[_gindex];
    [param setObject:model.id forKey:@"type"];
    [param setObject:body forKey:@"content"];
    
    if(!ValidArray(_uploadImage))
    {
        _uploadImage = @[IMAGE_NAMED(@"home_female_empty")];
    }

    kWeakSelf(self);
    
    [PPNetworkHelper uploadImagesWithURL:url parameters:param name:nil images:_uploadImage fileNames:nil imageScale:1 imageType:@"png" progress:^(NSProgress *progress) {
        
    } success:^(id responseObject) {
        [MBProgressHUD hideHUD];
        
        if([[responseObject valueForKey:@"code"] intValue] == 1)
        {
            [[HUDHelper sharedInstance] syncStopLoading];
            [MBProgressHUD showTopTipMessage:ASLocalizedString(@"举报成功")];
            [self.collectionView reloadData];
            [weakself.navigationController popViewControllerAnimated:YES];
        }
        else
        {
            
            [[HUDHelper sharedInstance] syncStopLoading];
            [MBProgressHUD showTipMessageInView:[responseObject valueForKey:@"msg"]];
        }
        
    } failure:^(NSError *error) {
        [[HUDHelper sharedInstance] syncStopLoading];
    }];
    
    
//    NSDictionary *params = @
//    {
//        @"touid":_touid,
//        @"body":[body stringByURLEncode],
//        @"type":[_type stringByURLEncode],
//        @"token":[UData getToken],
//        @"uid":[UData take].ID
//    };
//
//    url = [url stringByAppendingString:[utils Dic2Url:params]];
//
//    if(_uploadImage == nil)
//    {
//        [MBProgressHUD showError:ASLocalizedString(@"请选择图片")];
//        return;
//    }
//
//    [MBProgressHUD showMessage:ASLocalizedString(@"请稍后")];
//
//    [WZXNetworking updataVideoWithUrl:url withParams:nil withImage:_uploadImage withCompressionQuality:1 success:^(id response) {
//        [MBProgressHUD hideHUD];
//
//
//        if([[response valueForKey:@"code"] isEqualToString:@"0"])
//        {
//            [MBProgressHUD showSuccess:ASLocalizedString(@"举报成功")];
//            [self.navigationController popViewControllerAnimated:YES];
//        }
//        else
//        {
//            [MBProgressHUD showSuccess:[response valueForKey:@"msg"]];
//        }
//
//    } fail:^(NSError *error) {
//        [MBProgressHUD hideHUD];
//        //        [self.navigationController popViewControllerAnimated:NO];
//    }];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end

